package com.example.children.controller;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.example.children.DTO.AdminSelect;
import com.example.children.DTO.StudentClassDTO;
import com.example.children.DTO.TeacherSelect;
import com.example.children.Service.StudentService;
import com.example.children.Util.Tools;
import com.example.children.Util.UUIDGenerator;
import com.example.children.Util.tool;
import com.example.children.Model.Student;
import com.example.children.Model.ResponseModel;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.List;

@RestController
@RequestMapping("/students")
public class StudentController {

    @Autowired
    private StudentService studentService;

    @PostMapping("/add")
    public ResponseModel<Student> addStudent(@RequestBody Student student) {
        System.out.println("进入addStudent方法："+student.getName());
        Integer code= Tools.Success;
        String msg= tool.success;
        String uuid = UUIDGenerator.generateNumericUUID(8);
        student.setId(uuid);
        studentService.insertStudent(student);
        return new ResponseModel<>(student,code,msg,null);//响应给前端的对象
    }

    @GetMapping("/TeacherToAll")
    public ResponseModel<Page<StudentClassDTO>> getAllStudents(
            @RequestParam(defaultValue = "1") int pageNo,  // 默认页码为1
            @RequestParam(defaultValue = "10") int pageSize,
            @RequestParam String teacherId  // 默认每页10条
    ) {
        System.out.println("进入TeacherToAll方法："+teacherId);
        Integer code = Tools.Success;
        String msg = tool.success;
        // 调用 Service 方法进行分页查询
        Page<StudentClassDTO> studentPage = studentService.getStudentWithClassPage(pageNo, pageSize,teacherId);
        // 判断分页结果为空
        if (studentPage == null || studentPage.getRecords().isEmpty()) {
            msg = tool.fail;
        }
        // 返回分页数据
        return new ResponseModel<>(studentPage, code, msg, null);
    }

    @GetMapping("/AdminToAll")
    public ResponseModel<Page<StudentClassDTO>> AdminGetAllStudents(
            @RequestParam int pageNo,  // 默认页码为1
            @RequestParam int pageSize// 默认每页10条
    ) {
        System.out.println("进入AdminToAll方法：");
        Integer code = Tools.Success;
        String msg = tool.success;
        // 调用 Service 方法进行分页查询
        Page<StudentClassDTO> studentPage = studentService.getStudentWithClassPage(pageNo, pageSize);
        // 判断分页结果为空
        if (studentPage == null || studentPage.getRecords().isEmpty()) {
            msg = tool.fail;
        }
        // 返回分页数据
        return new ResponseModel<>(studentPage, code, msg, null);
    }




    @GetMapping("/all")
    public ResponseModel<List<StudentClassDTO>> getAllStudents() {
        System.out.println("进入all方法：");
        Integer code= Tools.Success;
        String msg= tool.success;
        List<StudentClassDTO> studentClassDTOS=studentService.getStudentWithClass();
        if(studentClassDTOS==null||studentClassDTOS==new ArrayList<StudentClassDTO>()||studentClassDTOS.isEmpty()){
            msg= tool.fail;
        }
        return new ResponseModel<>(studentClassDTOS,code,msg,null);//响应给前端的对象
    }



    @PutMapping("/update")
    public ResponseModel<Student> updateStudent(@RequestBody Student student) {
        System.out.println("进入update方法："+student.getName());
        Integer code= Tools.Success;
        String msg= tool.success;
        studentService.updateStudent(student);
        return new ResponseModel<>(student,code,msg,null);//响应给前端的对象
    }

    @DeleteMapping("/deleteStudent")  //删除
    public ResponseModel<Student> deleteStudent(@RequestParam String id) {
        System.out.println("进入delete方法："+id);
        Integer code= Tools.Success;
        String msg= tool.success;
        studentService.deleteStudent(id);
        return new ResponseModel<>(null,code,msg,null);//响应给前端的对象

    }



    // 日期搜索出勤记录
    @PostMapping("/selectByDateStudent")
    public ResponseModel<List<Student>> selectByDateStudent(@RequestBody Student student) {
        System.out.println("进入selectByDateStudent方法：" +student.getEnrollmentDate());
        Integer code= Tools.Success;
        String msg= tool.success;
        List<Student> students=studentService.selectByDateStudent(student);
        if(students == null || students.isEmpty())
            msg=tool.fail;
        return new ResponseModel<>(students, code, msg, null); // 成功响应
    }


    //查询
    @PostMapping("/ManySelectStudent")
    public ResponseModel<List<Student>> getStudentsByAge(@RequestBody TeacherSelect test) {
        System.out.println("进入teacherSelectStudent方法：" +test.getAgeStart());
        System.out.println("进入teacherSelectStudent方法：" +test.getAgeEnd());
        System.out.println("进入teacherSelectStudent方法：" +test.getGender());
        System.out.println("进入teacherSelectStudent方法：" +test.getGender());
        System.out.println("进入teacherSelectStudent方法：" +test.getName());
        Integer code= Tools.Success;
        String msg= tool.success;
        List<Student> students=studentService.age(test);
        if(students == null || students.isEmpty())
            msg=tool.fail;
        return new ResponseModel<>(students, code, msg, null); // 成功响应
    }

    //管理员查询
    @PostMapping("/AdminSelectStudent")
    public ResponseModel<List<Student>> AdminSelectStudent(@RequestBody AdminSelect test) {
        System.out.println("进入AdminSelectStudent方法：" +test.getAgeStart());
        System.out.println("进入AdminSelectStudent方法：" +test.getAgeEnd());
        System.out.println("进入AdminSelectStudent方法：" +test.getGender());
        System.out.println("进入AdminSelectStudent方法：" +test.getGender());
        System.out.println("进入AdminSelectStudent方法：" +test.getName());
        Integer code= Tools.Success;
        String msg= tool.success;
        List<Student> students=studentService.AdminSelectStudent(test);
        if(students == null || students.isEmpty())
            msg=tool.fail;
        return new ResponseModel<>(students, code, msg, null); // 成功响应
    }



}
